package com.taomu.app.zongchouhuli.moudle.home.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResMainProductListEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.moudle.home.contract.HomeSearchContract;
import com.taomu.app.zongchouhuli.moudle.home.contract.impl.HomeSearchPresenter;
import com.taomu.app.zongchouhuli.moudle.index.adpter.BaseProductAdp;
import com.taomu.app.zongchouhuli.moudle.index.entity.ProductEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.util.List;

import static com.taomu.app.zongchouhuli.moudle.home.activity.HomeProductDetailActivity.PRODUCTTYPEID;

/**
 * 搜索界面
 */
public class HomeSearchActivity extends BaseActivity<HomeSearchContract.Presenter> implements HomeSearchContract.View {

    private ImageView mBackIm;

    private EditText mSearchEt;

    private ImageView mSearchIm;

    private EmptyRecyclerView mSearchRecy;

    private BaseProductAdp mAdp;

    private SmartRefreshLayout mRefreshLayout;

    private HomeSearchContract.Presenter mPresenter = new HomeSearchPresenter(this);

    @Override
    protected void onDestroy() {
        mPresenter.detch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_home_search;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.home_search_back_im);
        mSearchEt = findViewById(R.id.home_search_input_et);
        mSearchIm = findViewById(R.id.home_search_im);
        mRefreshLayout = findViewById(R.id.search_refresh);
        mRefreshLayout.setEnableLoadMore(false);
        //mRefreshLayout.setEnableRefresh(false);
        mAdp = new BaseProductAdp(this);
        mSearchRecy = findViewById(R.id.home_search_recy);
        mSearchRecy.setLayoutManager(new LinearLayoutManager(this));
        mSearchRecy.setAdapter(mAdp);
        ListViewUtils.setDefaultEmpty(mSearchRecy);
    }

    @Override
    protected void initData() {

        mRefreshLayout.autoRefresh();
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSearchIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mSearchEt.getText().toString();
                if(!TextUtils.isEmpty(content)){
                    mRefreshLayout.setEnableRefresh(true);
                    mPresenter.getMoreProductList(content,String.valueOf(skip),String.valueOf(take));
                }else{
                    showToast("请输入关键字");
                }
            }
        });
        mAdp.setProductAdpListener(new BaseProductAdp.ProductAdpListener() {
            @Override
            public void onItemClick(ResMainProductListEntity.DataBean entity) {
                HomeProductDetailActivity.start(HomeSearchActivity.this, String.valueOf(entity.getId()));
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                skip = 0;
                mPresenter.getMoreProductList(getSeachContent(),String.valueOf(skip),String.valueOf(take));
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getMoreProductList(getSeachContent(),String.valueOf(skip),String.valueOf(take));
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });
        mSearchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){//搜索按键action
                        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(HomeSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                        final String content = mSearchEt.getText().toString();
                        if (!TextUtils.isEmpty(content)){
                            mRefreshLayout.setEnableRefresh(true);
                            mPresenter.getMoreProductList(content,String.valueOf(skip),String.valueOf(take));
                        }
                        return true;
                    }
                return false;
            }
        });

    }

    @Override
    protected HomeSearchContract.Presenter createPresenter() {
        return new HomeSearchPresenter(this);
    }

    @Override
    public void setPresenter(HomeSearchContract.Presenter presenter) {
        
    }

    private int skip = 0;

    private int take = 10;

    @Override
    public void setSearchList(ResMainProductListEntity mData) {
        if(mRefreshLayout.isLoading()){
            mAdp.addData(mData.getData());
        }else if(mRefreshLayout.isRefreshing()){
            mAdp.setData(mData.getData());
        }else{
            mAdp.setData(mData.getData());
        }
        skip = mAdp.getItemCount();
        if(Integer.valueOf(mData.getCount())>mAdp.getItemCount()){
            mRefreshLayout.setEnableLoadMore(true);
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    public void dismissLoadingDialog() {
        super.dismissLoadingDialog();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }

    private String getSeachContent(){
        return mSearchEt.getText().toString();
    }
}
